package com.onlinesshop.mall.controller;

import com.github.pagehelper.PageInfo;
import com.onlinesshop.mall.pojo.Images;
import com.onlinesshop.mall.pojo.Product;
import com.onlinesshop.mall.service.ImagesService;
import com.onlinesshop.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ImagesService imagesService;

    /**
     * 分类查找商品 并展示到Group_buy页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/group_buy")
    public ModelAndView findAll(ModelAndView model, @RequestParam("id") int id, @RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                @RequestParam(name = "page", required = true, defaultValue = "16") int size) {
        List<Product> byCategoryIds = productService.findByCategoryId(id, page, size);

        PageInfo byCategoryId = new PageInfo(byCategoryIds);

        model.addObject("byCategoryId", byCategoryId);
        model.setViewName("Group_buy");
        return model;
    }


    /**
     * 点击商品 根据id查找对应的的商品 并跳转到
     * @param modelAndView
     * @param id  产品的id
     * @param kind  根据不同条件排序 综合-1 销量-2  价格-3
     * @param page   显示第几页
     * @param size   每页显示几条数据
     * @return
     */
    @RequestMapping("/product_list")
    public ModelAndView findAllProductId(ModelAndView modelAndView, @RequestParam("id") int id, @RequestParam(name = "kind", required = true, defaultValue = "1") int kind, @RequestParam(name = "page", required = true, defaultValue = "1") int page, @RequestParam(name = "size", required = true, defaultValue = "16") int size) {
        PageInfo pageInfo = null;
        List<Product> products = null;
        if (kind == 1) {
            //根据分类id查询 如果没有传参数就默认根据分类id查询
            products = productService.findByCategoryId(id, page, size);
            //将查询的数据封装到分页对象中
            pageInfo = new PageInfo(products);
        } else if (kind == 2) {
            //根据库存量查询
            products = productService.findStock(id, page, size);
            //将查询的数据封装到分页对象中
            pageInfo = new PageInfo(products);

        } else if (kind == 3) {
            //根据价格查询
            products = productService.findGroupByPrice(id, page, size);
            //将查询的数据封装到分页对象中
            pageInfo = new PageInfo(products);
        }

        System.out.println(pageInfo.getPageSize());
        List<Product> products3 = selectRandom(3);
        /*保存当前产品id*/
        int productId = products.get(1).getCategory_id();

        /*将当前产品id保存到model*/
        modelAndView.addObject("productId", productId);
        //
        modelAndView.addObject("pageInfo", pageInfo);
        //随机查询三条数据 进行封装
        modelAndView.addObject("products3", products3);
        //执行方法的id
        modelAndView.addObject("kind",kind);
        //跳转页面
        modelAndView.setViewName("product_list");
        return modelAndView;
    }

    /**
     * 随机生成多条数据
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/findRandom3")
    public ModelAndView findRandom3(ModelAndView modelAndView) {

        return modelAndView;
    }

    /**
     * 查询产品的详细信息
     * @param modelAndView
     * @param id 产品的id
     * @return
     */
    @RequestMapping("/toProductDetaild")
    public ModelAndView productInformation(ModelAndView modelAndView,@RequestParam(name = "id",required = true,defaultValue = "1") int id){
        Product product = productService.findByProductId(id);
        List<Images> images = imagesService.findAllByProductId(id);
        modelAndView.addObject("productDetail",product);
        modelAndView.addObject("images",images);
        //跳转到产品详细信息页面
        modelAndView.setViewName("introduction");
        return  modelAndView;
    }



    /**
     * 查找多条数据 工具方法
     */
    public List<Product> selectRandom(int num) {
        List<Product> products = productService.findRandom(num);
        return products;
    }

}
